package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.pojo.*;
import cc.linker.steplink.crm.merchant.service.AssetCenterService;
import cc.linker.steplink.crm.merchant.service.NewMallService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资产中心
 * @author wuhf 2018-09-11
 */
@Controller
@RequestMapping("assetCenter/sop")
public class SopAssetCenterController {

    @Autowired
    private AssetCenterService assetCenterService;
    @Autowired
    private NewMallService newMallService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 获取账户总览
     * @return
     */
    @RequestMapping("/overview")
    @ResponseBody
    public BaseEntityDto<AssetCenterOverViewPo> overview(HttpServletRequest resRequest) {
        BaseEntityDto<AssetCenterOverViewPo> dto = new BaseEntityDto<AssetCenterOverViewPo>();
        try {
            AssetCenterOverViewPo assetCenterOverViewPo=new AssetCenterOverViewPo();
            assetCenterOverViewPo=assetCenterService.getAssetCenterOverView(resRequest);
            dto.setEntity(assetCenterOverViewPo);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("获取账户总览失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * 充值申请
     * @param rechargeNum 充值金额
     * @return
     */
    @RequestMapping("/recharge")
    @ResponseBody
    public BaseEntityDto<RechargeReceivablePo> recharge(Double rechargeNum, HttpServletRequest resRequest) {
        BaseEntityDto<RechargeReceivablePo> dto = new BaseEntityDto<RechargeReceivablePo>();
        try {
            RechargeReceivablePo receivablePo = assetCenterService.recharge(rechargeNum,resRequest);
            dto.setEntity(receivablePo);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("申请充值失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * 获取充值记录
     * @param
     * @return
     */
    @RequestMapping("/getRechargeList")
    @ResponseBody
    public BasePageDto<RechargeRecordPo> getRechargeList(Integer perPage, Integer pageIndex,
                                  String startTime, String endTime, Integer rechargeState, HttpServletRequest resRequest) {
        BasePageDto<RechargeRecordPo> dto = new BasePageDto<RechargeRecordPo>();
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("perPage",perPage);
            params.put("pageIndex",pageIndex);
            params.put("startTime",startTime);
            params.put("endTime",endTime);
            params.put("rechargeState",rechargeState);
            dto= assetCenterService.getRechargeList(params,resRequest);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("获取充值记录失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * 提交重置凭证
     * @param draweeAccount 付款人账号
     * @param draweeName 付款人姓名
     * @param payTime 支付时间
     * @param rechargeId 充值订单ID
     * @return
     */
    @RequestMapping("/rechargeVoucher")
    @ResponseBody
    public BaseDto rechargeVoucher(String draweeAccount,
                                  String draweeName,
                                  String payTime,
                                  Long rechargeId,
                                  HttpServletRequest resRequest) {
        BaseDto dto = new BaseDto();
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("rechargeId",rechargeId);
            params.put("payTime",payTime);
            params.put("draweeName",draweeName);
            params.put("draweeAccount",draweeAccount);
            assetCenterService.rechargeVoucher(params);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("提交充值凭证失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * 保存银行卡
     * @return
     */
    @RequestMapping("/saveBankCard")
    @ResponseBody
    public BaseDto saveBankCard(BankCardPo bankCardPo, HttpServletRequest resRequest) {
        BaseDto dto = new BaseDto();
        try {
            dto=assetCenterService.saveBankCard(bankCardPo,resRequest);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("新增银行卡失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * 获取银行卡
     * @return
     */
    @RequestMapping("/getBankCardList")
    @ResponseBody
    public BaseListDto<BankCardPo> getBankCardList(HttpServletRequest resRequest) {
        BaseListDto<BankCardPo> dto = new BaseListDto<BankCardPo>();
        try {
            List<BankCardPo> bankCardList=assetCenterService.getBankCardList(resRequest);
            dto.setCon(bankCardList);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("获取银行卡失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * 删除银行卡
     * @return
     */
    @RequestMapping("/delBankCard")
    @ResponseBody
    public BaseDto delBankCard(Long bankCardId, HttpServletRequest resRequest) {
        BaseDto dto = new BaseDto();
        try {
            assetCenterService.delBankCard(bankCardId,resRequest);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("删除银行卡失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * 设置默认银行卡
     * @return
     */
    @RequestMapping("/setDefaultBankCard")
    @ResponseBody
    public BaseDto setDefaultBankCard(Long bankCardId, Integer isDefault, HttpServletRequest resRequest) {
        BaseDto dto = new BaseDto();
        try {
            assetCenterService.setDefaultBankCard(bankCardId,isDefault,resRequest);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("设置默认银行卡失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * @Description: 提现
     */
    @RequestMapping("/doWithdraw")
    @ResponseBody
    public BaseDto doWithdraw(WithdrawInfoPo withdrawInfoPo, HttpServletRequest resRequest){
        BaseDto dto = new BaseDto();
        try {
            dto=assetCenterService.saveWithdraw(withdrawInfoPo,resRequest);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("申请提现失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * @Description: 获取提现记录
     */
    @RequestMapping("/getWithdrawList")
    @ResponseBody
    public BasePageDto<WithdrawInfoPo> getWithdrawList(Integer perPage, Integer pageIndex,
                                  String startTime, String endTime, Integer withdrawState,
                                  HttpServletRequest resRequest){
        BasePageDto<WithdrawInfoPo> dto = new BasePageDto<WithdrawInfoPo>();
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("perPage",perPage);
            params.put("pageIndex",pageIndex);
            params.put("startTime",startTime);
            params.put("endTime",endTime);
            params.put("withdrawState",withdrawState);
            dto = assetCenterService.getWithdrawList(params,resRequest);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setDes("sop获取提现失败");
            dto.setRt(0);
        }
        return dto;
    }

    /**
     * @Description: 获取账户明细
     */
    @GetMapping("/getTransactionDetails")
    @ResponseBody
    public PageInfo<TransactionDetailsPo> getTransactionDetails(@RequestParam(defaultValue = "20") Integer pageSize,
                                        @RequestParam(defaultValue = "1")Integer pageIndex,
                                        String startTime, String endTime,
                                        String tradeOrder, Integer tradeType,
                                                                HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("perPage",pageSize);
        params.put("pageIndex",pageIndex);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("tradeOrder",tradeOrder);
        params.put("tradeType",tradeType);
        params.put("orgId",userinfo.getOrgId());

        String userId = userinfo.getAdminId();
        String salerId = newMallService.getSalerId(userId);
        if (!StringUtils.isEmpty(salerId)) {
            params.put("salerId",salerId);
            Integer tradeSubType = -1;
            if (tradeType != null && tradeType != -1) {
                tradeSubType = tradeType == 11 ? 11 : (tradeType == 12 ? 12 : -1);
            }
            tradeType = 1;// 小b账单明细数据库为活动营销类型
            params.put("tradeType", tradeType);
            params.put("tradeSubType", tradeSubType);
        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<TransactionDetailsPo>(assetCenterService.getTransactionDetails(params));
    }

    /**
     * @Description: 获取账户汇总
     */
    @GetMapping("/getAccountSummary")
    @ResponseBody
    public PageInfo<AccountSummaryPo> getAccountSummary(@RequestParam(defaultValue = "20") Integer pageSize,
           @RequestParam(defaultValue = "1")Integer pageIndex, @RequestParam(defaultValue = "month")String scale, 
           String statisticalTime, HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> params = new HashMap<>();
        if(!StringUtils.isEmpty(statisticalTime)){
            if ("month".equals(scale)) {
                statisticalTime += "-01";
            } else if ("year".equals(scale)) {
                statisticalTime += "-02-01";
            } else {
                throw GlobalException.build("格式错误");
            }
        }
        params.put("perPage",pageSize);
        params.put("pageIndex",pageIndex);
        params.put("statisticalTime",statisticalTime);
        params.put("scale",scale);
        params.put("orgId",userinfo.getOrgId());

        String userId = userinfo.getAdminId();
        String salerId = newMallService.getSalerId(userId);
        if (!StringUtils.isEmpty(salerId)) {
            params.put("salerId",salerId);
        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(assetCenterService.getAccountSummary(params));
    }

}
